package com.lili.collection.jdbc.plugins;

import com.lili.collection.core.config.OutputConfig;
import com.lili.collection.core.config.ProcessConfig;
import com.lili.collection.core.context.BatchRecordContext;
import com.lili.collection.core.context.PipeContext;
import com.lili.collection.core.context.StageContext;
import com.lili.collection.core.fields.Field;
import com.lili.collection.core.plugins.ProcessPlugin;
import com.lili.collection.core.record.Record;
import com.lili.collection.jdbc.config.HikariPoolConfig;
import com.lili.collection.jdbc.enums.DatabaseVendor;
import com.lili.collection.jdbc.utils.JdbcCharUtil;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 *描述: 数据库字符处理插件
 *@date: 2023/7/3  10:44
 *@author: lili
 */
@Slf4j
public class DBCharProcessPlugin implements ProcessPlugin<ProcessConfig> {

    @Getter
    @Setter
    private PipeContext pipeContext;

    @Override
    public void init(ProcessConfig config) {
    }

    @Override
    public void setPipeContext(PipeContext pipeContext) {
        this.pipeContext = pipeContext;
    }

    @Override
    public StageContext execute(StageContext stageContext) {
        BatchRecordContext recordContext = (BatchRecordContext) stageContext;

        List<Record> output = recordContext.getOutput();
        if (output.size() == 0) {
            return stageContext;
        }
        try {
            //处理数据
            OutputConfig outputConfig = this.pipeContext.getContext().getOutputConfig();
            if(outputConfig instanceof HikariPoolConfig){
                HikariPoolConfig config= (HikariPoolConfig)outputConfig;
                String connectionString = config.getConnectionString();
                if(StringUtils.isBlank(connectionString)){
                    return stageContext;
                }
                DatabaseVendor databaseVendor = DatabaseVendor.forUrl(connectionString);
                if(!(DatabaseVendor.ORACLE==databaseVendor || DatabaseVendor.POSTGRESQL==databaseVendor)){
                    return stageContext;
                }
                for (Record record : output) {
                    Object value = record.getField().getValue();
                    if(!(value instanceof Map)){
                        continue;
                    }
                    Map<String, Field> rowData=(Map)value;
                    for (String name : rowData.keySet()) {
                        Field field = rowData.get(name);
                        Object obj = JdbcCharUtil.dateTypeHandle(field.getValue());
                        field.setValue(obj);
                        if(databaseVendor == DatabaseVendor.POSTGRESQL){
                            //数值转换
                            if(JdbcCharUtil.isInt(obj)){
                                field.setValue(Long.valueOf((String)obj));
                            }else if(JdbcCharUtil.isDouble(obj)){
                                field.setValue(new BigDecimal(obj.toString()));
                            }
                        }
                    }
                }
            }
        } catch (Exception exception) {
            log.error("处理异常!",exception);
            throw exception;
        }
        return stageContext;
    }


    @Override
    public void dispose() {

    }
}